from django.contrib import admin
from .models import Notification, NotificationSettings


@admin.register(Notification)
class NotificationAdmin(admin.ModelAdmin):
    """通知管理"""
    
    list_display = (
        'id', 'user', 'notification_type', 'title', 
        'is_read', 'created_at'
    )
    list_filter = ('notification_type', 'is_read', 'created_at')
    search_fields = ('user__username', 'title', 'content')
    ordering = ('-created_at',)
    
    fieldsets = (
        ('通知信息', {
            'fields': ('user', 'notification_type', 'title', 'content')
        }),
        ('关联信息', {
            'fields': ('related_object_type', 'related_object_id', 'extra_data'),
            'classes': ('collapse',)
        }),
        ('状态信息', {
            'fields': ('is_read', 'read_at')
        }),
    )
    
    readonly_fields = ('created_at', 'read_at')
    list_per_page = 20
    date_hierarchy = 'created_at'
    
    actions = ['mark_as_read', 'mark_as_unread']
    
    def mark_as_read(self, request, queryset):
        """批量标记为已读"""
        from django.utils import timezone
        count = queryset.update(is_read=True, read_at=timezone.now())
        self.message_user(request, f'已标记 {count} 条通知为已读')
    mark_as_read.short_description = '标记为已读'
    
    def mark_as_unread(self, request, queryset):
        """批量标记为未读"""
        count = queryset.update(is_read=False, read_at=None)
        self.message_user(request, f'已标记 {count} 条通知为未读')
    mark_as_unread.short_description = '标记为未读'


@admin.register(NotificationSettings)
class NotificationSettingsAdmin(admin.ModelAdmin):
    """通知设置管理"""
    
    list_display = (
        'id', 'user', 'order_notifications', 'comment_notifications', 
        'like_notifications', 'system_notifications', 'email_notifications'
    )
    search_fields = ('user__username',)
    ordering = ('user',)
    
    fieldsets = (
        ('用户信息', {
            'fields': ('user',)
        }),
        ('通知开关', {
            'fields': (
                'order_notifications', 'comment_notifications', 'like_notifications',
                'follow_notifications', 'system_notifications', 'price_change_notifications',
                'group_notifications', 'email_notifications'
            )
        }),
    )
    
    readonly_fields = ('created_at', 'updated_at')
    list_per_page = 20
